#!/usr/bin/perl

use Test;

BEGIN { plan tests => 3 }

$basedir = $0;
$basedir =~ s|(.*)/[^/]*|$1|;

my %conf;
my $confpath = $basedir . "/ibendport_test.conf";
open( $f, $confpath ) or die("Couldn't open ibtest.conf");
while ( $r = <$f> ) {
    if ( $r =~ /^\s*#/ || $r =~ /^\s*$/ ) { next; }
    chomp $r;
    ( $k, $v ) = split( /=/, $r );
    $conf{$k} = $v;
}

# verify that we really want to run these tests
ok( $conf{SELINUX_INFINIBAND_ENDPORT_TEST} eq 1 );

@allowed_device_port =
  split( /,/, $conf{SELINUX_INFINIBAND_ENDPORT_TEST_ALLOWED} );
@denied_device_port =
  split( /,/, $conf{SELINUX_INFINIBAND_ENDPORT_TEST_DENIED} );

foreach (@allowed_device_port) {
    @dev_port_pair = split( / /, $_ );

    system "semanage ibendport -a -t test_ibendport_t -z $_ 2>/dev/null";
    $result = system
"runcon -t test_ibendport_manage_subnet_t smpquery PKeyTable -C $dev_port_pair[0] -P $dev_port_pair[1] -D 1 2>/dev/null";
    system "semanage ibendport -d -t test_ibendport_t -z $_ 2>/dev/null";
    if ( $result ne 0 ) {
        last;
    }
}
ok( $result, 0 );

foreach (@denied_device_port) {
    @dev_port_pair = split( / /, $_ );
    $result        = system
"runcon -t test_ibendport_manage_subnet_t smpquery PKeyTable -C $dev_port_pair[0] -P $dev_port_pair[1] -D 1 2>/dev/null";

    if ( $result >> 8 eq 0 ) {
        last;
    }
}

ok( int( $result >> 8 ) ne 0 );

exit;
